package cn.pzhu.common;

import cn.pzhu.controller.admin.*;
import cn.pzhu.controller.web.*;
import cn.pzhu.handler.GlobalHandler;
import cn.pzhu.model.*;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.tx.TxByActionKeyRegex;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;

/**
 * Created by dongp on 2016/4/21.
 */
public class MainConfig extends JFinalConfig{
    /**
     * 配置常量
     *
     *
     */
    public void configConstant(Constants me) {
        // 加载少量必要配置，随后可用PropKit.get(...)获取值
        PropKit.use("init.properties");

        //设置为开发模式，在开发模式下每次会输出 controller  method 以及携带参数
        me.setDevMode(PropKit.getBoolean("devMode", false));

        //设置视图模式，默认视图模式是freemark
		me.setViewType(ViewType.JSP);

        me.setError500View("/WEB-INF/view/admin/common/500.jsp");
        me.setError404View("/WEB-INF/view/admin/common/404.jsp");
//        me.setBaseViewPath("/WEB-INF/view");

    }

    /**
     * 配置路由
     */
    public void configRoute(Routes me) {
        me.add("/admin", cn.pzhu.controller.admin.IndexController.class, "/WEB-INF/view/admin");	// 第三个参数为该Controller的视图存放路径
        me.add("/admin/", cn.pzhu.controller.admin.IndexController.class, "/WEB-INF/view/admin");	// 第三个参数为该Controller的视图存放路径
        me.add("/admin/user", UserController.class,"/WEB-INF/view/admin/user");
        me.add("/admin/role/dropDown", RoleController.class,"/WEB-INF/view/admin/user/user.jsp");
        me.add("/admin/role", RoleController.class,"/WEB-INF/view/admin/role");
        me.add("/admin/privilege", PrivilegeController.class,"/WEB-INF/view/admin/privilege");
        me.add("/admin/achievement", AchievementController.class, "/WEB-INF/view/admin/achievement");
        me.add("/admin/category", CategoryController.class, "/WEB-INF/view/admin/category");
        me.add("/admin/demand", DemandController.class, "/WEB-INF/view/admin/demand");
        me.add("/admin/college", CollegeController.class, "/WEB-INF/view/admin/college");
        me.add("/admin/company", CompanyController.class, "/WEB-INF/view/admin/company");
        me.add("/admin/expert", ExpertController.class, "/WEB-INF/view/admin/expert");

        me.add("/",  cn.pzhu.controller.web.IndexController.class,"/web/page");
        me.add("/web/validCode",  ValidCodeController.class);
        me.add("/web/login",  AccountController.class,"/web/page");
        me.add("/web/acheievement",  WebAcheievementController.class,"/web/page");
        me.add("/web/demand",  WebDemandController.class,"/web/page");
        me.add("/web/order",  WebOrderController.class,"/web/page");
        me.add("/web/member",  WebMemberController.class,"/web/page");
        me.add("/web/expert",  WebExpertController.class,"/web/page");
        me.add("/web/college",  WebCollegeController.class,"/web/page");
        me.add("/web/company",  WebCompanyController.class,"/web/page");
    }

    public static C3p0Plugin createC3p0Plugin() {
        return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
    }

    /**
     * 配置插件
     */
    public void configPlugin(Plugins me) {
        // 配置C3p0数据库连接池插件
        C3p0Plugin C3p0Plugin = createC3p0Plugin();
        me.add(C3p0Plugin);

        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(C3p0Plugin);
        arp.setShowSql(true);
        me.add(arp);

        //配置Model与Table映射
        arp.addMapping("user","userID",User.class);
        arp.addMapping("role","roleID", Role.class);
        arp.addMapping("privilege","privilegeID", Privilege.class);
        arp.addMapping("issueacheievement","acheievementID", Achievement.class);
        arp.addMapping("category","categoryID", Category.class);
        arp.addMapping("issuedemand","demandID", Demand.class);
        arp.addMapping("logistics","logisticsID", Logistics.class);
        arp.addMapping("goods","orderID", Order.class);
        arp.addMapping("member","memberID", Member.class);
        arp.addMapping("expert","expertID", Expert.class);
        arp.addMapping("colleges","collegesID", College.class);
        arp.addMapping("company","companyID", Company.class);
    }

    /**
     * 配置全局拦截器
     */
    public void configInterceptor(Interceptors me) {
        me.add(new TxByActionKeyRegex("(.*add*.*)|(.*delete*.*)|(.*edit*.*)|(.*update*.*)|(.*cancel*.*)|(.*create*.*)"));
    }

    /**
     * 配置处理器
     */
    public void configHandler(Handlers me) {
        me.add(new GlobalHandler());
    }

    /**
     * 建议使用 JFinal 手册推荐的方式启动项目
     * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
     *
     * 参数一次为  根目录    端口  上下文    估计是超时时间
     */
    public static void main(String[] args) {
        JFinal.start("WebRoot", 8080, "/", 5);
    }
}
